let tools = {};

/**
 * [toArray 类数组对象转数组]
 * @param  {[Array-like]} list  [类数组]
 * @param  {[Number]} index [起始索引]
 * @return {[Array]}       [返回一个新的真实数组]
 */
tools.toArray = function(list,index){
	index = index || 0
	let i = list.length - index
	let _array = new Array(i)
	while(i--){
		_array[i] = list[i+index]
	}
	return _array
}

/**
 * [isArray 判断是否为数组]
 * @type {Boolean}
 */
tools.isArray = Array.isArray

let toString = Object.prototype.toString
let OBJECT_TYPE = '[object Object]'
/**
 * [isPlainObject 判断是否为普通对象]
 * @param  {[Object]}  obj [对象]
 * @return {Boolean} 
 */
tools.isPlainObject = function(obj){
	return toString.call(obj) === OBJECT_TYPE
}

/**
 * [isObject 判断是否为对象]
 * @param  {[*]}  obj [任意一个元素]
 * @return {Boolean}
 */
tools.isObject = function(obj){
	return obj !== null && typeof obj === 'object'
}

/**
 * [toType 导出类型字符串]
 * @param  {[type]} value [description]
 * @return {[type]}       [description]
 */
tools.toType = function(value){
	return toString.call(value)
}

/**
 * [indexOf 查询一个元素在数组中的下标]
 * @param  {[Array]} arr [查询的目标数组]
 * @param  {[*]} obj [任意元素]
 * @return {[Number]}     [查询成功返回下标不成功则返回-1]
 */
tools.indexOf = function(arr,obj){
	let i = arr.length
	while(i--){
		if (arr[i] === obj) {
			return i
		}
	}
	return -1
}

/**
 * [extend 将一个对象的属性添加到另一个对象中]
 * @param  {[Object]} to   [目标对象]
 * @param  {[Object]} from [将此对象属性添加到目标对象中]
 * @return {[Object]}      
 */
tools.extend = function(to,from){
	let keys = Object.keys(from)
	let i = keys.length
	while(i--){
		to[keys[i]] = from[keys[i]]
	}
	return to
}

let hasOwnProperty = Object.prototype.hasOwnProperty
/**
 * [hasOwn 检查对象是否为自身的属性]
 * @param  {[Object]}  obj [description]
 * @param  {[String]}  key [description]
 * @return {Boolean}     [description]
 */
tools.hasOwn = function(obj,key){
	return hasOwnProperty.call(obj,key)
}

/**
 * [exportToString 导出字符串]
 * @param  {[*]} value [description]
 * @return {[String]}       [description]
 */
tools.exportToString = function(value){
	return value === null ? '' : value.toString()
}

/**
 * [exportToNumber 导出数字]
 * @param  {[*]} value [description]
 * @return {[*|Number]}       [description]
 */
tools.exportToNumber = function(value){
	if (typeof value !== 'string') {
		return value
	}else{
		let number = Number(value)
		return isNaN(number) ? value : number
	}
}

/**
 * [exportToBoolean 导出布尔值]
 * @param  {[*]} value [description]
 * @return {[*|Number]}       [description]
 */
tools.exportToBoolean = function(value){
	return value === 'true' ? true : value === 'false' ? false : value
}

/**
 * [set 合并对象]
 * @param {[type]} obj   [description]
 * @param {[type]} key   [description]
 * @param {[type]} value [description]
 */
tools.set = function(obj,key,value){
	if (tools.hasOwn(obj,key)) {
		//如果key是obj自身的属性，添加到obj中
		obj[key] = value
		return 
	}

	if (obj._isWine) {
		//如果obj对象为Wine实例，继续调用set方法加入到_store中
		set(obj._store,key,value)
		return
	}

	let ob = obj.__ob__
	if (!ob) {
		//如果不是观察者对象，直接添加到obj中
		obj[key] = value
		return
	}

	//如果是观察者对象
	
	return value
}

/**
 * [mergeData 合并数据对象]
 * @param  {[type]} to   [description]
 * @param  {[type]} from [description]
 * @return {[type]}      [description]
 */
tools.mergeData = function(to,from){
	let toValue,fromValue
	for(let key in from){
		toValue = to[key]
		fromValue = from[key]
		if (!tools.hasOwn(to,key)) {
			//如果key是to自身的属性，将fromValue加入到to中
			tools.set(to,key,fromValue)
		}else if(tools.isObject(toValue) && tools.isObject(fromValue)){
			//如果toValue和fromValue依然是object，继续递归
			tools.mergeData(toValue,fromValue)
		}
	}
	return to
}

module.exports = tools;